<template>
  <div class="question-container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="学科名称">
              <el-input style="200px" v-model="subjectsObj.subjectName" ></el-input>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="subjectsObj.subjectName=''">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button
          icon="el-icon-edit"
          type="success"
          size="small"
          @click="addSubjects"
          >新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="subjectsList" >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="学科名称" prop="subjectName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" width="160px">
           <template #default={row}>{{row.addDate|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template #default={row}>{{row.isFrontDisplay===1?'是':'否'}}</template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
        <el-table-column label="标签" prop="tags"></el-table-column>
        <el-table-column label="题目数量" prop="totals"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template #default = {row}>
            <!-- {{row}} -->
            <el-button type="text" @click="$router.push(`/subjects/directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
            <el-button type="text" @click="$router.push(`/subjects/tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
            <el-button type="text" @click="editSubjects(row)" >修改</el-button>
            <el-button type="text" @click="delSubjects(row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="subjectsObj.pagesize"
        :current-page="subjectsObj.page"
        :page-sizes="[5,10,20,50]"
        background
      ></el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <subjects-add ref="subjectsadd" :list ='list' ></subjects-add>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '../components/subjects-add'
export default {
  name: 'SubjectsList',
  components: { SubjectsAdd },
  data () {
    return {
      list: {}, // 科学详情信息
      subjectsList: [],
      total: 1, // 列表总数
      subjectsObj: {
        subjectName: '',
        page: 1, // 当前页数
        pagesize: 10 // 每页条数
      }
    }
  },
  created () {
    this.getList()
  },
  // 获取数据
  methods: {
    async getList () {
      const { data } = await list(this.subjectsObj)
      // console.log(data)
      this.subjectsList = data.items
      this.total = data.counts
    },
    // 切换条数
    handleSizeChange (size) {
      this.subjectsObj.page = 1
      this.subjectsObj.pagesize = size
      this.getList()
    },
    // 切换分页
    handleCurrentChange (val) {
      this.subjectsObj.page = val
      this.getList()
    },
    // 搜索
    search () {
      this.subjectsObj.page = 1
      this.getList()
    },
    // 打开新增对话框
    async addSubjects () {
      this.$refs.subjectsadd.show = true
    },
    // 修改
    async editSubjects (str) {
      this.list = str || {}
      // 异步操作完成后 再执行
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
      // console.log(this.$refs.subjectsadd.list)
    },
    // 删除学科
    async delSubjects (id) {
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消操作')
      await remove({ id })
      if (this.subjectsList.length === 1 && this.subjectsObj.page > 1) {
        this.subjectsObj.page--
      }
      this.getList()
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
